package com.game.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.game.business.domain.GroupMember;
import com.game.business.service.HxSevice;
import com.game.business.service.IGroupMemberService;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.enums.BusinessType;
import com.game.common.utils.SecurityUtils;
import com.game.common.utils.StringUtils;
import com.game.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.business.domain.TGroup;
import com.game.business.service.ITGroupService;
import com.game.common.core.page.TableDataInfo;

/**
 * 群聊Controller
 *
 * @author ruoyi
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/business/group")
public class TGroupController extends BaseController {
    @Autowired
    private ITGroupService tGroupService;

/**
 * 查询群聊列表
 */
//@RequiresPermissions("business:group:list")
@GetMapping("/list")
    public TableDataInfo list(TGroup tGroup) {
        startPage();
        List<TGroup> list = tGroupService.selectTGroupList(tGroup);
        return getDataTable(list);
    }

    /**
     * 导出群聊列表
     */
    //@RequiresPermissions("business:group:export")
    @Log(title = "群聊", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGroup tGroup) {
        List<TGroup> list = tGroupService.selectTGroupList(tGroup);
        ExcelUtil<TGroup> util = new ExcelUtil<TGroup>(TGroup. class);
        util.exportExcel(response, list, "群聊数据");
    }

    /**
     * 获取群聊详细信息
     */
    //@RequiresPermissions("business:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tGroupService.getById(id));
    }


    @Autowired
    private HxSevice hxSevice;

    @Autowired
    private IGroupMemberService groupMemberService;
    /**
     * 新增群聊
     */
    //@RequiresPermissions("business:group:add")
    @Log(title = "群聊", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGroup vo) {
        vo.setType("2");
        String userIdStr = SecurityUtils.getUserIdStr();
        String hxGroupId = hxSevice.createGroup(vo.getGroupName(), vo.getDescription(),userIdStr);
        if(StringUtils.isEmpty(hxGroupId)){
            throw  new RuntimeException("创建群聊失败");
        }
        vo.setGroupId(hxGroupId);
        vo.setOwnerId(userIdStr);
        tGroupService.save(vo);
        // 添加环信一个成员
        hxSevice.addMembersToGroup(hxGroupId,userIdStr);
        // 添加一个成员
        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(hxGroupId);
        groupMember.setRole("2");
        groupMember.setUserId(userIdStr);
        groupMemberService.save(groupMember);
        return AjaxResult.success(vo);
    }

    /**
     * 修改群聊
     */
    //@RequiresPermissions("business:group:edit")
    @Log(title = "群聊", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGroup tGroup) {
        return toAjax(tGroupService.updateById(tGroup));
    }

    /**
     * 删除群聊
     */
    //@RequiresPermissions("business:group:remove")
    @Log(title = "群聊", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            TGroup byId = tGroupService.getById(ids[i]);
            String groupId = byId.getGroupId();
            tGroupService.removeById(ids[i]);
        }
        return AjaxResult.success();
    }
}
